package labox.innovation.gameserver.datatables;

import java.util.Date;
import java.util.List;

import javolution.util.FastList;
import labox.innovation.gameserver.script.DateRange;

/**
 * 这个类管理被GM创建的特殊掉落事件在定义的时间内.
 * 在特殊的掉落事件时间中，L2Attackable能掉出额外的道具.
 * 这些额外的道具被定义在表 <B>allNpcDateDrops</B>中.
 * 每一个特殊掉落事件都有开始和结束的日期去自动的停止掉落.<BR><BR>
 */

public class EventDroplist {

	//private static Logger _log = Logger.getLogger(EventDroplist.class.getName());

	/** The table containing all DataDrop object */
	private List<DateDrop> _allNpcDateDrops;

	public static EventDroplist getInstance() {
		return SingletonHolder._instance;
	}

	public class DateDrop {
		/** Start and end date of the Event */
		public DateRange dateRange;

		/** The table containing Item identifier that can be dropped as extra Items during the Event */
		public int[] items;

		/** The min number of Item dropped in one time during this Event */
		public int min;

		/** The max number of Item dropped in one time during this Event */
		public int max;

		/** The rate of drop for this Event */
		public int chance;

	}

	/**
	 * Constructor of EventDroplist.<BR><BR>
	 */
	private EventDroplist() {
		_allNpcDateDrops = new FastList<DateDrop>();
	}

	/**
	 * Create and Init a new DateDrop then add it to the allNpcDateDrops of EventDroplist .<BR><BR>
	 *
	 * @param items The table containing all item identifier of this DateDrop
	 * @param count The table containing min and max value of this DateDrop
	 * @param chance The chance to obtain this drop
	 * @param range The DateRange object to add to this DateDrop
	 *
	 */
	public void addGlobalDrop(int[] items, int[] count, int chance, DateRange range) {

		DateDrop date = new DateDrop();

		date.dateRange = range;
		date.items = items;
		date.min = count[0];
		date.max = count[1];
		date.chance = chance;

		_allNpcDateDrops.add(date);
	}

	/**
	 * Return all DateDrop of EventDroplist allNpcDateDrops within the date range.<BR><BR>
	 */
	public List<DateDrop> getAllDrops() {
		List<DateDrop> list = new FastList<DateDrop>();

		for (DateDrop drop : _allNpcDateDrops) {
			Date currentDate = new Date();
			//_log.info("From: "+drop.from+" To: "+drop.to+" Now: "+ currentDate);
			if (drop.dateRange.isWithinRange(currentDate)) {
				list.add(drop);
			}
		}

		return list;
	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final EventDroplist _instance = new EventDroplist();
	}
}
